package fr.alma.domo.music.impl;


import java.util.UUID;

import org.oasisopen.sca.annotation.Reference;
import org.oasisopen.sca.annotation.Service;

import fr.alma.domo.model.AbstractService;
import fr.alma.domo.model.Context;
import fr.alma.domo.music.ClientContext;
import fr.alma.domo.music.PositionService;
import fr.alma.domo.svc.ContextHandler;
import fr.alma.domo.util.Log;

@SuppressWarnings("serial")
@Service(PositionService.class)
public class PositionServiceImpl extends AbstractService implements PositionService{

	@Reference(required=true)
	protected ContextHandler contextHandler;

	public PositionServiceImpl(){
		super("PositionService");
	}
	
	/**
	 * Recherche d'un context client par son nom
	 * @param clientId
	 * @return
	 */
	public ClientContext getClientContextById(String clientId){	
		if(contextHandler==null){
			Log.i(PositionService.class.getName()+"#getClientContextById", "The context handler is missing");
			return null;
		}
		Context ctx = contextHandler.getContextById(clientId);
		if(ctx instanceof ClientContext){
			return (ClientContext)ctx;
		}
		return null;
	}

	/**
	 * Recherche un context client par son nom
	 * @param clientName
	 * @return
	 */
	private ClientContext getClientContextByName(String clientName){
		if(contextHandler==null){
			Log.i(PositionService.class.getName()+"#getClientContextByName", "The context handler is missing");
			return null;
		}
		
		for (Context context:  contextHandler.getContexts().values()){
			if  (context instanceof ClientContext && ((ClientContext)context).getName().equals(clientName)){
				return (ClientContext) context;				
			}
		}				
		return null;
	}
	
	@Override
	public String getPositionById(String clientId) {
//		System.out.println("\n"+contextHandler.getContexts()+"\n");
		ClientContext ctx = getClientContextById(clientId);		
		if(ctx==null){
			Log.i(PositionService.class.getName()+"#getPositionById", "No such client [%s]",clientId);
			return NO_POSITION;
		}
		return ctx.getCurrentPosition()==null?NO_POSITION:ctx.getCurrentPosition();
	}


	@Override
	public String getClientId(String nameClient) {		
		//On recherche si le client est deja enregistré
		ClientContext ctx = getClientContextByName(nameClient);
		
		//Sinon on lui cree un context
		if(ctx==null){
			if(contextHandler==null){
				Log.i(PositionService.class.getName()+"#getClientId", "The context handler is missing");
				return null;
			}
			ctx = new ClientContext();
			ctx.setName(nameClient);
			ctx.setId(UUID.randomUUID().toString());
			ctx.setCurrentPosition(PositionService.NO_POSITION);
			contextHandler.addContext(ctx);
			Log.i(PositionService.class.getName()+"#getClientId", String.format("Registering new client [%s] with id [%s]",ctx.getName(),ctx.getId()));
		}
		
		return ctx.getId();
	}

	@Override
	public void setClientPosition(String clientId, String newPosition) {
		ClientContext ctx = getClientContextById(clientId);
		if(ctx!=null){
			ctx.setCurrentPosition(newPosition);
			//on met a jour le context
			contextHandler.addContext(ctx);
			//System.out.println("\n"+contextHandler.getContexts()+"\n");
		}
		else{
			Log.i(PositionService.class.getName()+"#setClientPosition", "No such client [%s]",clientId);
		}
	}
	
}
